package vn.info.vietnaminfo.adapter;

import java.util.ArrayList;
import java.util.List;

import vn.info.vietnaminfo.PlaceDetailActivity;
import vn.info.vietnaminfo.R;
import vn.info.vietnaminfo.data.DSDiaDiem;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DanhDauAdapter extends BaseAdapter
{
	private Context mContext;
	private LayoutInflater mInflater;	
	private List<DSDiaDiem> listDiaDiem = null;
	
	public DanhDauAdapter(Context context, List<DSDiaDiem> object)
	{
		mContext 	= context;
		listDiaDiem	= object;
		mInflater  	= LayoutInflater.from(context);
	}
	
	public class ViewHolder
	{
		TextView txtTieuDe;
		TextView txtTenCuaHang;
		TextView txtDiaChi;
		LinearLayout layout;
	}

	public View getView(final int position, View view, ViewGroup parent)
	{
		final ViewHolder holder;
		if (view == null)
		{
			holder 					= new ViewHolder();
			view 					= mInflater.inflate(R.layout.layout_item_address, null);
			holder.txtTieuDe 		= (TextView)view.findViewById(R.id.item_dd_title);					
			holder.txtTenCuaHang 	= (TextView)view.findViewById(R.id.item_dd_ten_ch);
			holder.txtDiaChi 		= (TextView)view.findViewById(R.id.item_dd_ten_dc);
			holder.layout 			= (LinearLayout)view.findViewById(R.id.item_dd_layout);
			view.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) view.getTag();
		}
		
		if(listDiaDiem.get(position).getTitle())
		{
			holder.txtTieuDe.setTag(position);
			holder.txtTieuDe.setText(listDiaDiem.get(position).getTenLoaiKD());
			
			holder.txtTieuDe.setVisibility(View.VISIBLE);
			holder.layout.setVisibility(View.GONE);
		}
		else
		{
			holder.txtTenCuaHang.setTag(position);
			holder.txtTenCuaHang.setText(listDiaDiem.get(position).getTenDiaDiem());
			
			String diachi = listDiaDiem.get(position).getDiaChi();
			
			holder.txtDiaChi.setTag(position);
			holder.txtDiaChi.setText(diachi);
			
			holder.layout.setTag(position);
			holder.layout.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{
					Intent intent = new Intent(mContext, PlaceDetailActivity.class);
					intent.putExtra("POSITION", position);
					intent.putParcelableArrayListExtra("DS_DIA_DIEM", (ArrayList<? extends Parcelable>) listDiaDiem);
					mContext.startActivity(intent);
				}
			});
			
			holder.txtTieuDe.setVisibility(View.GONE);
			holder.layout.setVisibility(View.VISIBLE);
		}
		return view;
	}
	
	public int getCount()
	{
		return listDiaDiem.size();
	}

	public Object getItem(int position)
	{
		return position;
	}

	public long getItemId(int position)
	{
		return position;
	}
}